package com.jackstraw.jack_base.datasource.elasticsearch.entity.bean;

import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.transport.ElasticsearchTransport;
import com.jackstraw.jack_base.datasource.common.entity.AbstractTemplate;
import com.jackstraw.jack_base.datasource.elasticsearch.exception.BadElasticSearchException;
import lombok.Data;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;

import java.io.IOException;


/**
 * es服务类
 */
@Data
public class ElasticSearchTemplate extends AbstractTemplate<ElasticSearchTemplate, ElasticSearchPropertiesEntity> {


    private RestClientBuilder restClientBuilder;

    private RestClient restClient;

    private ElasticsearchTransport elasticsearchTransport;

    private ElasticsearchClient elasticsearchClient;


    private ElasticsearchAsyncClient elasticsearchAsyncClient;

    private SimpleElasticsearchMappingContext simpleElasticsearchMappingContext;


    @Override
    public ElasticsearchClient get() {
        return this.elasticsearchClient;
    }


    /**
     * 配置文件内默认查询数量
     *
     * @return
     */
    public Integer getQuerySize() {
        ElasticSearchPropertiesEntity propertiesEntity = this.getPropertiesEntity();
        return propertiesEntity.getQuerySize();
    }

    @Override
    public void destroy() {
        try {
            elasticsearchTransport.close();
            restClient.close();
        } catch (IOException e) {
            throw new BadElasticSearchException("040", "关闭流异常", e);
        }

    }

}
